package com.wnhz.work.yz.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wnhz.work.yz.MainActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.BathActivity;
import com.wnhz.work.yz.adapter.ClassifyAdapter;
import com.wnhz.work.yz.bean.ClassifyBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * ---家居馆
 */
public class Home1Classify extends Fragment {
    private View view;
    private RecyclerView recycler;
    private ClassifyAdapter mAdapter;
    private List<ClassifyBean> list = new ArrayList<>();
    private MainActivity mainActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home1_classify, container, false);
        mainActivity = (MainActivity) getActivity();
        initData();
        initView();
        return view;
    }

    private void initData() {

    }

    private void initView() {
        recycler = (RecyclerView) view.findViewById(R.id.recycler);
        upload();
        recycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mAdapter = new ClassifyAdapter(getActivity(), list);
        recycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new ClassifyAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int data) {
                Intent intent = new Intent(getActivity(), BathActivity.class);
                intent.putExtra("titles", list.get(data).getTitle());
                intent.putExtra("cateId", list.get(data).getCateId());
                startActivity(intent);

            }
        });
    }

    private static final String TAG = "Home1Classify";

    private void upload() {
        mainActivity.showDialog();
        XUtil.Post(Url.GOODS_GETGOODSCATE, null, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                mainActivity.closeDialog();
                super.onSuccess(result);
                Log.i(TAG, "onSuccess: frag2" + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
//                    if ("-1".equals(res)) {
//                        //info = "请重新登录";
//                        mainActivity.MyToast("请重新登录");
//                        startActivity(new Intent(mainActivity, LoginNewActivity.class));
//                        return;
//                    }

                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject j = jsonArray.getJSONObject(i);
                            ClassifyBean bean = new ClassifyBean();
                            String cateId = j.optString("cateId");
                            String pic = j.optString("pic");
                            String title = j.optString("title");

                            bean.setCateId(cateId);
                            bean.setPic(pic);
                            bean.setTitle(title);

                            list.add(bean);
                        }
                    }
                    mAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                mainActivity.closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mainActivity.closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

}
